package com.ruoyi.web.controller.pc.anon;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.blogs.domain.BsBrowseHistory;
import com.ruoyi.blogs.domain.BsResource;
import com.ruoyi.blogs.service.IBsBrowseHistoryService;
import com.ruoyi.blogs.service.IBsResourceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "资源")
@RestController
@RequestMapping("/anon/resource")
public class AnonResourceController extends BaseController {
    
    @Autowired
    private IBsResourceService bsResourceService;

    @Autowired
    private IBsBrowseHistoryService bsBrowseHistoryService;

    /**
     * 查询资源列表
     */
    @ApiOperation("获取资源列表")
    @GetMapping("/list")
    public TableDataInfo list(BsResource bsResource) {
        startPage();
        bsResource.setVisible(0);
        List<BsResource> list = bsResourceService.selectBsResourceList(bsResource);
        return getDataTable(list);
    }

    /**
     * 获取资源详细信息
     */
    @ApiOperation("获取资源详细信息")
    @GetMapping
    public R<BsResource> getInfo(@RequestParam("id") String id) {
        BsResource bsResource = bsResourceService.selectBsResourceById(id);
        try {
            // 添加历史记录
            BsBrowseHistory bsBrowseHistory = new BsBrowseHistory();
            bsBrowseHistory.setConnectId(id);
            bsBrowseHistory.setType(2);
            bsBrowseHistory.setTitle(bsResource.getTitle());
            bsBrowseHistoryService.insertBsBrowseHistory(bsBrowseHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(bsResource);
    }

}
